from PIL import Image as img
import os

def test(horizon=4, vertic =1):
    '''批量切图片,切当前目录和子目录'''
    imgTypes = ['.png', '.jpg', '.bmp']
    for root, dirs, files in os.walk('.'):
        for currentFile in files:
            crtFile = root + '\\' + currentFile
            if crtFile[crtFile.rindex('.'):].lower() in imgTypes:
                crtIm = img.open(crtFile)
                crtW, crtH = crtIm.size
                hStep = crtW // horizon
                vStep = crtH // vertic
                for i in range(vertic):
                    for j in range(horizon):
                        crtOutFileName = crtFile[:crtFile.rindex('.')] + \
                            '_' + str(i) + '_' + str(j)\
                            + crtFile[crtFile.rindex('.'):].lower()
                        box = (j * hStep, i * vStep, (j + 1) * hStep, (i + 1) * vStep)
                        cropped = crtIm.crop(box)
                        cropped.save(crtOutFileName)

def spilt(imgFile, H=4, V=1):
    ImgObj = img.open(imgFile)
    crtW, crtH = ImgObj.size
    hStep = crtW // H
    vStep = crtH // V
    for i in range(V):
        for j in range(H):
            OutFileName = imgFile[:imgFile.rindex('.')] + \
                             '_' + str(i) + '_' + str(j) \
                             + imgFile[imgFile.rindex('.'):].lower()
            box = (j * hStep, i * vStep, (j + 1) * hStep, (i + 1) * vStep)
            print(box)
            print(OutFileName)
            cropped = ImgObj.crop(box)
            cropped.save(OutFileName)
